var MaterialClassifyModel = function (materialTypeList, materialOption) {
  BaseModel.call(this);

  if (materialOption === undefined) {
    materialOption = {};
  }

  var self = this;
  var fn = {
    initData: function (materialTypeList, materialOption) {
      var option = _.cloneDeep(MaterialClassifyModel.defaultOption);
      _.assign(option, materialOption);
      self.option = option;

      var treeObj = {};
      var classifyList = [];
      var keyField = option.keyField,
        parentField = option.parentField,
        childrenField = option.childrenField;
      for (var i = 0; i < materialTypeList.length; i++) {
        var materialType = materialTypeList[i];
        treeObj[materialType[keyField]] = materialType;
      }

      for (var id in treeObj) {
        var materialType = treeObj[id];
        var parentId = materialType[parentField];
        if (parentId !== null) {
          var parentObj = treeObj[parentId];
          if (!parentObj) {
            continue;
          }
          if (parentObj[childrenField] === undefined) {
            parentObj[childrenField] = [];
          }
          parentObj[childrenField].push(materialType);
        } else {
          classifyList.push(materialType);
        }
      }

      self.arrayData = materialTypeList;
      self.treeData = classifyList;
      self.treeObj = treeObj;
    }
  };

  this.getTreePath = function (id) {
    if (SysUtil.isEmpty(id)) {
      return [];
    }

    var option = this.option;
    var parentField = option.parentField;

    var treeObj = this.treeObj;
    var node = treeObj[id];
    var treePath = [];
    while (node !== undefined) {
      treePath.push(node);
      var parentId = node[parentField];
      if (SysUtil.isEmpty(parentId)) {
        break;
      }
      node = treeObj[parentId];
    }
    treePath.reverse();
    return treePath;
  };

  fn.initData(materialTypeList, materialOption)
};

MaterialClassifyModel.defaultOption = {
  keyField: 'id',
  parentField: 'parentId',
  childrenField: 'children'
};